package com.hxd.mall.module.account.bindphone

import android.graphics.Color
import android.text.TextUtils
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.hxd.mall.R
import com.hxd.mall.base.BaseInjectActivity
import com.hxd.mall.constant.HxdConstants.IntentParamsKeyDefine.login_or_register_phone_num
import com.hxd.mall.constant.HxdConstants.IntentParamsKeyDefine.login_or_register_type
import com.hxd.mall.constant.HxdConstants.IntentParamsKeyDefine.register_invite_code
import com.hxd.mall.constant.HxdConstants.from_register_to_verifyCode
import com.hxd.mall.constant.HxdConstantsRouterPath.PathAccount.path_bind_phone
import com.hxd.mall.constant.HxdConstantsRouterPath.PathAccount.path_verify_code
import com.hxd.mall.module.account.bean.InvitationCodeBean
import com.hxd.mall.module.account.register.IBindPhonePresenter
import com.hxd.mall.module.account.register.IBindPhoneView
import com.hxd.mall.module.usercenter.settings.bean.SettingBean
import com.hxd.mall.utils.local.*
import com.hxd.mall.utils.local.NavigationUtil.routerNavigation
import com.hxd.mall.utils.open.ToastUtil
import com.hxd.mall.utils.open.addSetOnClickListener
import com.hxd.mall.wxapi.BindWxUtils
import kotlinx.android.synthetic.main.activity_bindphone_layout.*
import kotlinx.android.synthetic.main.activity_register_layout.inputInviteCode
import kotlinx.android.synthetic.main.activity_register_layout.next
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe

/**
 * @see 绑定微信界面
 *
 */
@Route(path = path_bind_phone)
class BindPhoneActivity : BaseInjectActivity<IBindPhonePresenter>(), IBindPhoneView {

    private lateinit var phoneNum: String

    /**
     * 邀请码是否输入完成
     */
    private var inputFinish = false
    /**
     * 勾选框是否勾选
     */
    private var boxSelected = true

    override fun attachLayoutRes(): Int = R.layout.activity_bindphone_layout

    override fun initViews() {
        setTitleCommon("绑定微信").run {
            setTitleRightJustText("跳过")
            setTitleRightTextColor(resources.getColor(R.color.color_E248400))
            setOnRightClickedListener{
                //跳过
                StackManageUtil.loginOut(this@BindPhoneActivity)
                ToastUtil.showToast("登录成功")
                sendPoint(skip_button_click)
            }
        }
            .setOnLeftClickedListener{
                ToastUtil.showToast("登录成功")
                StackManageUtil.loginOut(this@BindPhoneActivity)
            }
        inputInviteCode.inputFinish { finish ->
            inputFinish = finish
            changeNextBtnStatus()
        }
        //设置了setOnClickListener后，强制clickable为true,xml中的false失效，需要在代码中设置
        next.isClickable = false

        sendPoint(bounding_wechat_page_view)

        addSetOnClickListener(iv_bind_wx,next){
            addOnClick(it)
        }
        EventBus.getDefault().register(this)
    }

    private fun addOnClick(it: View) {
        when(it){
            next->{
                if (!TextUtils.isEmpty(inputInviteCode.getInputContent())) {
                    //校验手机号是否注册过
                    mPresenter?.invitationCode(inputInviteCode.getInputContent())
                }
            }
            iv_bind_wx->{
                //绑定微信
                sendPoint(bounding_wechat_button_click)
                BindWxUtils.getParamsFromService(this)
            }

        }
    }

    override fun initData() {
        phoneNum = intent.getStringExtra(login_or_register_phone_num) ?: ""
    }

    override fun invitationCodeResult(t: InvitationCodeBean) {
        routerNavigation(
            path_verify_code, hashMapOf(
                login_or_register_phone_num to phoneNum,
                login_or_register_type to from_register_to_verifyCode,
                register_invite_code to inputInviteCode.getInputContent()
            )
        )
    }


    private fun changeNextBtnStatus() {
        if (inputFinish && boxSelected) {
            next.isClickable = true
            next.setBackgroundResource(R.drawable.shape_btn_able_bg)
        } else {
            next.isClickable = false
            next.setBackgroundColor(Color.parseColor("#55ffffff"))
        }
    }

    override fun onBackPressed() {
        super.onBackPressed()
        StackManageUtil.loginOut(this@BindPhoneActivity)
        ToastUtil.showToast("登录成功")
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    @Subscribe
    fun updataWxAuther(event: SettingBean){
        StackManageUtil.loginOut(this)
    }
}